package com.luoy.util;

import java.io.*;
import java.util.Enumeration;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

/**
 * 读取zip文件中指定文件的数据
 *
 * @author LUOY
 * @create 2018-06-29-上午11:12
 */
public class GetZipFileContentUtil {

    public static String get(File zipFile, String readFileName) throws ZipException, IOException {

        StringBuilder content = new StringBuilder();
        ZipFile zip = new ZipFile(zipFile);
        Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip.entries();

        ZipEntry ze;
        // 枚举zip文件内的文件/
        while (entries.hasMoreElements()) {
            ze = entries.nextElement();
            // 读取目标对象
            if (ze.getName().equals(readFileName)) {
                Scanner scanner = new Scanner(zip.getInputStream(ze));
                while (scanner.hasNextLine()) {
                    content.append(scanner.nextLine());
                }
                scanner.close();
            }
        }
        zip.close();
        return content.toString();
    }
}
